package cn.luckdou.commons.utils;

import cn.luckdou.system.service.sys_config.ISysConfigService;

/**
 *
 * Dadou
 */
public class SysConfigUtils {
    //SpringBean 注入
    private static ISysConfigService ISysConfigService = SpringContextHolder.getBean(ISysConfigService.class);


    /**
     * 获取系统配置（SysConfig）
     * @param key
     * @return value
     */
    public static String getValue(String key) {
        if (StringUtils.isNotBlank(key)){
            JedisUtil.Strings strings = JedisUtil.getInstance().new Strings();
            byte[] bytes =null;
            try{
                bytes = strings.get(("SysConfig_"+key).getBytes());
            }catch (Exception e){
            }
            if (bytes != null) {
                return (String) SerializeUtil.unserialize(bytes);
            } else {
                String value = ISysConfigService.getValue(key);
                try{
                    String set = strings.set("SysConfig_"+key, SerializeUtil.serialize(value));
                }catch (Exception e){
                }
                return value;
            }
        }
        return null;
    }


    /**
     * 重置redis中的值
     * @param key
     * @return
     */
    public static boolean resetValue(String key) {
        if (StringUtils.isNotBlank(key)) {
            try{
                String value = ISysConfigService.getValue(key);
                JedisUtil.Strings strings = JedisUtil.getInstance().new Strings();
                String set = strings.set("SysConfig_"+key, SerializeUtil.serialize(value));
                return true;
            }catch (Exception e){
                return false;
            }
        }
        return false;
    }
}
